import axios from 'axios'
import {Message, MessageBox} from 'element-ui'
import Cookies from 'js-cookie'

//创建axios 实例
const service = axios.create({
	// baseURL : 'https://easy-mock.com/mock/5c6f5a45e1bf2b67057e3ebc/api',
	timeout:500000
})

//axios request拦截器配置
service.interceptors.request.use(config => {
	//设置每次请求携带x-token
	if(Cookies.get('x-token')){
		config.headers['x-token'] = Cookies.get('x-token')
	}
	return config
},error => {
	Message({
		message:'请求超时5000ms'+ error.message,
		type:'error',
	})
})


//axios response拦截器    // 30000 请求成功，50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;{code:50008,message:'非法TOKEN'}
service.interceptors.response.use(response => {
	//过滤服务器无权限操作
	if(response.status == 200){
		const res = response.data
		console.log(res)
		if (res.code !== 30000) {  
			Message({
			  message: res.msg,
			  type: 'error',
			  duration: 5 * 1000
			})
			console.log(res.data)
		}else{
			return response
		}
	}else{
		Message({
			message:'响应失败（拦截器AXIOS）',
			type:'error',
			duration: 5 * 1000
		})
	}
},error => {
	Message({
		message:'响应失败（拦截器AXIOS）'+ error.message,
		type:'error',
		duration: 5 * 1000
	})
})
export default service



